public class Solution2 {
    public boolean backspaceCompare1(String s, String t) {
        return changeStr(s).equals(changeStr(t));
    }

    public String changeStr(String s) {
        StringBuffer ret = new StringBuffer();
        for(int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch != '#') {
                ret.append(ch);
            }else {
                if(ret.length() > 0) {
                    ret.deleteCharAt(ret.length() - 1);
                }
            }
        }
        return ret.toString();
    }

    public boolean backspaceCompare(String ss, String tt) {
        if(ss.equals(tt)) {
            return true;
        }
        StringBuffer ret_s = new StringBuffer();
        StringBuffer ret_t = new StringBuffer();
        char[] s = ss.toCharArray();
        char[] t = tt.toCharArray();
        for(int i = 0; i < s.length; i++) {
            if(s[i] == '#') {
                if(ret_s.length() > 0) {
                    ret_s.deleteCharAt(ret_s.length() - 1);
                }
            }else {
                ret_s.append(s[i]);
            }
        }
        for(int i = 0; i < t.length; i++) {
            if(t[i] == '#') {
                if(ret_t.length() > 0) {
                    ret_t.deleteCharAt(ret_t.length() - 1);
                }

            }else {
                ret_t.append(t[i]);
            }
        }
        if(ret_s.toString().equals(ret_t.toString())) {
            return true;
        }else {
            return false;
        }
    }
}
